package com.epfresh.supplier.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputFilter;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.epfresh.api.global.BaseActivity;
import com.epfresh.api.utils.VerificationUtils;
import com.epfresh.supplier.R;

public class ProductEditActivity extends BaseActivity {


    private EditText edInput;


    private int minLen = 0;
    private int maxLen = 200;

    private int type = -1;
    private String hint = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (savedInstanceState != null){
            getIntent().putExtra("type",savedInstanceState.getInt("type"));
            getIntent().putExtra("content",savedInstanceState.getString("content"));
        }

        setContentView(R.layout.activity_product_edit);



        edInput = (EditText) findViewById(R.id.ed_input);
        type = getIntent().getIntExtra("type", -1);
        if (type == 1) {//商品标题设置
            minLen = 4;
            maxLen = 25;
            hint = "4-25个字";
            toolbarTitle.setText(R.string.product_title);
        } else if (type == 2) {//品牌设置
            minLen = 0;
            maxLen = 10;
            hint = "不超过10个字";
            toolbarTitle.setText(R.string.product_brand);
        } else if (type == 3) {//产地设置
            minLen = 0;
            maxLen = 10;
            hint = "不超过10个字";
            toolbarTitle.setText(R.string.product_area);
        } else {
            finish();
        }

        edInput.setHint(hint);
        edInput.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLen)});

        edInput.setText(getIntent().getStringExtra("content"));
        edInput.setSelection(edInput.length());
        rightbtn.setText("保存");
        rightbtn.setVisibility(View.VISIBLE);

        ivBack.setVisibility(View.VISIBLE);
        tvBack.setVisibility(View.VISIBLE);


    }


    @Override
    public void onClick(View view) {
        super.onClick(view);

        switch (view.getId()) {
            case R.id.rightbtn:
                save();
                break;
        }

    }

    public void save() {
        String s = edInput.getText().toString();
        if (s.length() == 0) {
            toast("请输入内容");
            return;
        }
        if (s.length() < minLen) {
            toast("请输入" + minLen + "个字以上");
            return;
        }
        if (!VerificationUtils.isValid(s)){
            toast(getString(R.string.illegal_input,toolbarTitle.getText().toString()));
            return;
        }
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(edInput.getWindowToken(), 0); //强制隐藏键盘
        Intent intent = new Intent();
        intent.putExtra("result", s);
        setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    protected void onToolbarBack() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(edInput.getWindowToken(), 0); //强制隐藏键盘
        super.onToolbarBack();
    }

    @Override
    public void onBackPressed() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(edInput.getWindowToken(), 0); //强制隐藏键盘
        super.onBackPressed();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {


        outState.putInt("type",type);
        outState.putString("content",edInput.getText().toString());
        super.onSaveInstanceState(outState);
    }


}
